package bb.lanxing.manager;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import com.umeng.analytics.pro.an;
import bb.lanxing.App;
import bb.lanxing.util.Log;
import java.util.ArrayList;
import java.util.List;

public class StepCounterManager implements SensorEventListener {
    private static final int MICROSECONDS_IN_ONE_MINUTE = 60000000;
    private static final int MAX_TIME_REPORT = 10000000;//10 seconds
    private static final String TAG = "StepCounterManager";
    private static StepCounterManager instance;
    private float mLimit;
    private int mStepOffset;
    private int mSteps;
    private float mYOffset;
    private float[] sensitivities;
    private float[] mLastValues = new float[6];
    private float[] mScale = new float[2];
    private float[] mLastDirections = new float[6];
    private float[][] mLastExtremes = {new float[6], new float[6]};
    private float[] mLastDiff = new float[6];
    private int mLastMatch = -1;
    private boolean open = false;
    private List<StepChangedListener> stepChangedListeners = new ArrayList();

    public interface StepChangedListener {
        void onStepChanged(int step);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public static StepCounterManager getInstance() {
        if (instance == null) {
            instance = new StepCounterManager();
        }
        return instance;
    }

    public static void release() {
        StepCounterManager stepCounterManager = instance;
        if (stepCounterManager != null) {
            stepCounterManager.destroy();
        }
        instance = null;
    }

    private StepCounterManager() {
        float[] fArr = {1.9753f, 2.963f, 4.4444f, 6.6667f, 10.0f, 15.0f, 22.5f, 33.75f, 50.625f};
        this.sensitivities = fArr;
        this.mLimit = fArr[4];
        setupCounter();
    }

    private void destroy() {
        if (this.open) {
            closeCounter();
        }
        this.stepChangedListeners.clear();
    }

    public boolean openCounter() {
        Sensor defaultSensor;
        Log.i(TAG, "openCounter...");
        if (this.open) {
            return true;
        }
        this.mSteps = 0;
        SensorManager sensorManager = (SensorManager) App.getContext().getSystemService(Context.SENSOR_SERVICE);
        Log.i(TAG, "openCounter...sensorManager=" + sensorManager);
        if (Build.VERSION.SDK_INT >= 19 && (defaultSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)) != null) {
            this.mStepOffset = -1;
            sensorManager.registerListener(this, defaultSensor, SensorManager.SENSOR_DELAY_NORMAL, MAX_TIME_REPORT);//todo: will change to 1 minute
            this.open = true;
            Log.i(TAG, "start step count sensor");
            return true;
        }
        Sensor defaultSensor2 = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        Log.i(TAG, "openCounter...defaultSensor2=" + defaultSensor2);
        if (defaultSensor2 != null) {
            sensorManager.registerListener(this, defaultSensor2, SensorManager.SENSOR_DELAY_NORMAL);//SENSOR_DELAY_FASTEST
            this.open = true;
            Log.i(TAG, "start step count by accelerometer sensor");
            return true;
        }
        return false;
    }

    public void closeCounter() {
        if (this.open) {
            ((SensorManager) App.getContext().getSystemService(Context.SENSOR_SERVICE)).unregisterListener(this);
        }
    }

    private void setupCounter() {
        float f = 480 * 0.5f;
        this.mYOffset = f;
        float[] fArr = this.mScale;
        fArr[0] = -(0.05098581f * f);
        fArr[1] = -(f * 0.016666668f);
    }

    public void setSensitivity(int level) {
        this.mLimit = this.sensitivities[level];
    }

    public void registerListener(StepChangedListener listener) {
        synchronized (this) {
            this.stepChangedListeners.add(listener);
        }
    }

    public void unregisterListener(StepChangedListener listener) {
        synchronized (this) {
            this.stepChangedListeners.remove(listener);
        }
    }

    private void notifyListener() {
        for (StepChangedListener stepChangedListener : this.stepChangedListeners) {
            stepChangedListener.onStepChanged(this.mSteps);
        }
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor sensor = sensorEvent.sensor;
        Log.d(TAG, "onSensorChanged...sensor.getType()=" + sensor.getType());
        if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
            Log.d(TAG, "onSensorChanged... sensorEvent.values[0]=" + sensorEvent.values[0]);
            if (sensorEvent.values[0] > 2.14748365E9f) {
                Log.w(TAG, "probably not a real value: " + sensorEvent.values[0]);
                return;
            }
            int i = (int) sensorEvent.values[0];
            Log.w(TAG, "onSensorChanged...i=" + i);
            if (i <= 0) {
                return;
            }
            int i2 = this.mStepOffset;
            Log.d(TAG, "onSensorChanged...i2=" + i2);
            if (i2 < 0) {
                this.mStepOffset = i;
            } else {
                this.mSteps = i - i2;
            }
            notifyListener();
            return;
        }
//        char c = sensor.getType() == Sensor.TYPE_ACCELEROMETER ? (char) 1 : (char) 0;
//        if (c != 1) {
        if (sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
            return;
        }
        Log.d(TAG, "onSensorChanged...ACCELEROMETER");
        float f = 0.0f;
        for (int i3 = 0; i3 < 3; i3++) {
            f += this.mYOffset + (sensorEvent.values[i3] * this.mScale[1]);
        }
        float f2 = f / 3.0f;
        float[] fArr = this.mLastValues;
        float f3 = f2 > fArr[0] ? 1 : f2 < fArr[0] ? -1 : 0;
        if (f3 == (-this.mLastDirections[0])) {
            int i4 = f3 > 0.0f ? 0 : 1;
            float[][] fArr2 = this.mLastExtremes;
            fArr2[i4][0] = fArr[0];
            int i5 = 1 - i4;
            float abs = Math.abs(fArr2[i4][0] - fArr2[i5][0]);
            if (abs > this.mLimit) {
                float[] fArr3 = this.mLastDiff;
                boolean z = abs > (fArr3[0] * 2.0f) / 3.0f;
                boolean z2 = fArr3[0] > abs / 3.0f;
                boolean z3 = this.mLastMatch != i5;
                if (z && z2 && z3) {
                    this.mSteps++;
                    notifyListener();
                    this.mLastMatch = i4;
                } else {
                    this.mLastMatch = -1;
                }
            }
            this.mLastDiff[0] = abs;
        }
        this.mLastDirections[0] = f3;
        this.mLastValues[0] = f2;
        Log.d(TAG, "onSensorChanged...ACCELEROMETER mLastDirections[0]=" + mLastDirections[0] + ", mLastValues[0]=" + mLastValues[0]);
    }
}
